package com.ubxtech.core.utils;

/**
 * 研究院地图坐标转化
 * pixelX 像素X坐标
 * pixelY 像素Y坐标
 * resolution 地图分辨率 像素代表多少地图单位，所用地图的地图单位是米。
 * mapX 地图x坐标
 * mapY 地图y坐标
 * originX 地图原点X坐标
 * originY 地图原点Y坐标
 * bmpX 表示地图坐标系与pixel坐标系中O点的X轴差值，单位：像素
 * bmpY 表示地图坐标系与pixel坐标系中O点的Y轴差值，单位：像素
 * bmpW 表示地图图片的宽度，单位：像素
 * bmpH 表示地图图片的高度，单位：像素

 *
 * @author Victor.Xiao
 * @since 2023-12-25 10:36
 **/
public class CoordinateUtil {

    /**
     * 从像素坐标转化为地图坐标
     * @author Victor.Xiao
     * @since 2023-12-25 10:47
     * @param pixelX pixelX
     * @param resolution resolution
     * @param bmpX bmpX
     * @return double
    */
    public static double getMapX(int pixelX,double resolution,int bmpX) {
        return (pixelX + bmpX) * resolution;
    }

    /**
     * 从像素坐标转化为地图坐标
     * @author Victor.Xiao
     * @since 2023-12-25 10:47
     * @param pixelY pixelY
     * @param resolution resolution
     * @param bmpY bmpY
     * @return double
     */
    public static double getMapY(int pixelY,double resolution,int bmpY) {
        return (bmpY - pixelY) * resolution;
    }


    /**
     * 从地图坐标转化为像素坐标
     * @author Victor.Xiao
     * @since 2023-12-25 10:47
     * @param mapX mapX
     * @param resolution resolution
     * @param bmpX bmpX
     * @return int
     */
    public static int getPixelX(double mapX,double resolution,int bmpX) {
        return Math.abs((int)Math.round(mapX / resolution - bmpX));
    }

    /**
     * 从地图坐标转化为像素坐标
     * @author Victor.Xiao
     * @since 2023-12-25 10:47
     * @param mapY mapY
     * @param resolution resolution
     * @param bmpY bmpY
     * @return int
     */
    public static int getPixelY(double mapY,double resolution,int bmpY) {
        return Math.abs((int)Math.round(mapY / resolution - bmpY));
    }


    /**
     * 从地图坐标转化为像素坐标
     * @author Victor.Xiao
     * @since 2023-12-25 10:47
     * @param mapX mapX
     * @param resolution resolution
     * @param originX originX
     * @return int
     */
    public static int getPixelX(double mapX,double resolution,double originX) {
        return Math.abs((int)Math.round((mapX - originX) / resolution));
    }

    /**
     * 从地图坐标转化为像素坐标
     * @author Victor.Xiao
     * @since 2023-12-25 10:47
     * @param mapY mapY
     * @param resolution resolution
     * @param originY originY
     * @param height height
     * @return int
     */
    public static int getPixelY(double mapY,double resolution,double originY,int height) {
        return Math.abs((int)Math.round((mapY - height * resolution - originY) / resolution));
    }

    /**
     *
     * @author Victor.Xiao
     * @since 2023-12-25 10:55
     * @param resolution resolution
     * @param originX originX
     * @return double
    */
    public static int getBmpX(double resolution,double originX) {
        return (int)Math.round(originX / resolution);
    }

    /**
     *
     * @author Victor.Xiao
     * @since 2023-12-25 10:56
     * @param resolution resolution
     * @param originY originY
     * @param height height
     * @return double
    */
    public static int getBmpY(double resolution,double originY,int height) {
        return (int)Math.round(height + originY / resolution);
    }

//    public static void main(String[] args) {
//        double resolution=0.05D;
//        double originX=-19.200000762939453D;
//        double originY=-38.400001525878906D;
//        int height = 1024;
//        int bmpY = 256;
//        int bmpX = -384;
//
//        double mapX =-7.6D;
//        double mapY=-15.0;
//
//        int pixelX =232;
//        int pixelY =555;
//
//        System.out.println("BmpX,Y:" + CoordinateUtil.getBmpX(resolution, originX) + ":" + CoordinateUtil.getBmpY(resolution, originY, height));
//        System.out.println("Pixel X,Y:" + CoordinateUtil.getPixelX(mapX, resolution, bmpX) + ":" + CoordinateUtil.getPixelY(mapY, resolution, bmpY));
//        System.out.println("Pixel origin X,Y:" + CoordinateUtil.getPixelX(mapX, resolution, originX) + ":" + CoordinateUtil.getPixelY(mapY, resolution, originY, height));
//        System.out.println("mapX,Y:" + CoordinateUtil.getMapX(pixelX, resolution, bmpX) + ":" + CoordinateUtil.getMapY(pixelY, resolution, bmpY));
//    }
}
